package com.example.human_resource_management.controller;

import com.example.human_resource_management.common.ResponseData;
import com.example.human_resource_management.config.UserContextUtils;
import com.example.human_resource_management.entry.dto.AttendanceEmployeeDto;
import com.example.human_resource_management.entry.po.Attendance;
import com.example.human_resource_management.entry.po.Employee;
import com.example.human_resource_management.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/attendance")
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;
    //员工查询个人打卡信息
    @GetMapping("AttSe")
    public ResponseData<List<Attendance>> getAttendanceInfo() {
        Long empId = UserContextUtils.getEmpId();
        if (empId != null) {
            List<Attendance> attendances = attendanceService.getAttenById(empId);
            if (attendances != null && !attendances.isEmpty()) {
                return ResponseData.ok(attendances);
            }
            return ResponseData.warn("未查询到打卡记录");
        }
        return ResponseData.warn("未获取到员工信息");
    }

    //员工打卡
    //当前时间打卡
    @PostMapping("punch")
    public ResponseData<String> punch() {
        return attendanceService.addAttendanceRecord(LocalDateTime.now());
    }

    //管理员端
    // 联查考勤和员工信息
    @GetMapping("/list")
    public ResponseData<List<AttendanceEmployeeDto>> selectAttendanceEmployeeList() {
        List<AttendanceEmployeeDto> list = attendanceService.selectAttendanceEmployeeList();
        return ResponseData.ok(list);
    }

    // 按ID联查考勤和员工信息
    @GetMapping("/{recordId}")
    public ResponseData<AttendanceEmployeeDto> selectAttendanceEmployeeById(@PathVariable Long recordId) {
        AttendanceEmployeeDto dto = attendanceService.selectAttendanceEmployeeById(recordId);
        return ResponseData.ok(dto);
    }

    // 按日期查询考勤记录
    @GetMapping("/date")
    public ResponseData<List<AttendanceEmployeeDto>> selectAttendanceByDate(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
        List<AttendanceEmployeeDto> list = attendanceService.selectAttendanceByDate(date);
        return ResponseData.ok(list);
    }

    // 新增考勤记录
    @PostMapping("/add")
    public ResponseData<String> addAttendance(@RequestBody Attendance attendance) {
        boolean success = attendanceService.addAttendance(attendance);
        return success ? ResponseData.ok("新增考勤记录成功") : ResponseData.warn("新增考勤记录失败");
    }

    // 修改考勤记录
    @PutMapping("/update")
    public ResponseData<String> updateAttendance(@RequestBody Attendance attendance) {
        boolean success = attendanceService.updateAttendance(attendance);
        return success ? ResponseData.ok("修改考勤记录成功") : ResponseData.warn("修改考勤记录失败");
    }

    // 删除考勤记录
    @DeleteMapping("/delete/{recordId}")
    public ResponseData<String> deleteAttendance(@PathVariable Long recordId) {
        boolean success = attendanceService.deleteAttendance(recordId);
        return success ? ResponseData.ok("删除考勤记录成功") : ResponseData.warn("删除考勤记录失败");
    }
}
